package se.janlindblom.ordning.helper;

/**
 * $Id: Databas.java 127 2010-02-15 09:58:17Z lindblom.jan $
 * 
 * Copyright (c) 2009, Jan Lindblom
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the project nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import se.janlindblom.ordning.typ.Uppgift;
import se.janlindblom.ordning.typ.Kategori;
import se.janlindblom.ordning.typ.samling.Aktiviteter;
import se.janlindblom.ordning.typ.samling.Kategorier;

/**
 * Databas - database of {@link Uppgift} and {@link Kategori} objects.
 * 
 * @author Jan Lindblom (lindblom.jan@gmail.com)
 * @version $Rev: 127 $
 */
public class Databas {
	private Connection conn;
	private Aktiviteter activities;
	private Kategorier categories;
	private Uppgift currentActivity;
	private Kategori currentCategory;

	/**
	 * Creates a new Databas database.
	 * 
	 * @throws ClassNotFoundException
	 */
	public Databas() throws ClassNotFoundException {
		Class.forName("org.sqlite.JDBC");
	}
	
	/**
	 * Opens the connection of this Databas database.
	 * 
	 * @throws SQLException
	 */
	public void open() throws SQLException {
		String databaseName = "ordning.db";
		this.open(databaseName);
	}

        public void init() throws SQLException, DatabasException {
            if (conn == null) {
                throw new DatabasException("Connection to database not open.");
            }
             Statement stat = conn.createStatement();
             stat.executeUpdate("drop table if exists profile;");
             stat.executeUpdate("drop table if exists p2p;");
             stat.executeUpdate("drop table if exists project;");
             stat.executeUpdate("drop table if exists p2e;");
             stat.executeUpdate("drop table if exists entries;");
             stat.executeUpdate("create table profile (profile_id, profile_name);");
        }
	
	/**
	 * 
	 * @param database the database to open
	 * @throws SQLException
	 */
	public void open(String database) throws SQLException {
		conn = DriverManager.getConnection("jdbc:sqlite:" + database);
	}
	
	/**
	 * Closes the connection of this Databas database.
	 * 
	 * @throws SQLException
	 */
	public void close() throws SQLException {
		conn.close();
	}

        public class DatabasException extends Exception {
            public DatabasException() {
                super();
            }
            public DatabasException(String s) {
                super(s);
            }
        }
}
